import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _class;

import React from 'react';
import { Form, InputNumber, Modal } from 'antd';
import { Bind } from 'lodash-decorators';
import Switch from 'components/Switch';
import EditTable from 'components/EditTable';
import { Button as ButtonPermission } from 'components/Permission';
import { operatorRender, enableRender } from 'utils/renderer';
import intl from 'utils/intl';
import { tableScrollWidth } from 'utils/utils';
var FormItem = Form.Item;
var UserModal = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(UserModal, _React$Component);

  var _super = _createSuper(UserModal);

  function UserModal() {
    var _this;

    _classCallCheck(this, UserModal);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      selectedRowKeys: [],
      selectRows: []
    };
    return _this;
  }

  _createClass(UserModal, [{
    key: "handleSelectTable",
    value: function handleSelectTable(keys, rows) {
      this.setState({
        selectedRowKeys: keys,
        selectRows: rows
      });
    }
  }, {
    key: "handleDelete",
    value: function handleDelete() {
      var _this$props$onDelete = this.props.onDelete,
          onDelete = _this$props$onDelete === void 0 ? function (e) {
        return e;
      } : _this$props$onDelete;
      var _this$state = this.state,
          selectRows = _this$state.selectRows,
          selectedRowKeys = _this$state.selectedRowKeys;
      onDelete(selectRows.filter(function (item) {
        var temp = item;
        delete temp._status;
        return temp;
      }), selectedRowKeys);
      this.setState({
        selectedRowKeys: []
      });
    }
    /**
     * 编辑当前行
     * @param {Object} record - 当前行数据
     */

  }, {
    key: "editRow",
    value: function editRow(record, flag) {
      var _this$props$onEdit = this.props.onEdit,
          onEdit = _this$props$onEdit === void 0 ? function (e) {
        return e;
      } : _this$props$onEdit;
      onEdit(record, flag);
    }
  }, {
    key: "handleCancel",
    value: function handleCancel() {
      var _this$props$onCancel = this.props.onCancel,
          onCancel = _this$props$onCancel === void 0 ? function (e) {
        return e;
      } : _this$props$onCancel;
      this.setState({
        selectRows: [],
        selectedRowKeys: []
      });
      onCancel();
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          path = _this$props.path,
          _this$props$initLoadi = _this$props.initLoading,
          initLoading = _this$props$initLoadi === void 0 ? false : _this$props$initLoadi,
          _this$props$saveLoadi = _this$props.saveLoading,
          saveLoading = _this$props$saveLoadi === void 0 ? false : _this$props$saveLoadi,
          _this$props$deleteLoa = _this$props.deleteLoading,
          deleteLoading = _this$props$deleteLoa === void 0 ? false : _this$props$deleteLoa,
          _this$props$dataSourc = _this$props.dataSource,
          dataSource = _this$props$dataSourc === void 0 ? [] : _this$props$dataSourc,
          _this$props$modalVisi = _this$props.modalVisible,
          modalVisible = _this$props$modalVisi === void 0 ? false : _this$props$modalVisi,
          _this$props$onCancel2 = _this$props.onCancel,
          onCancel = _this$props$onCancel2 === void 0 ? function (e) {
        return e;
      } : _this$props$onCancel2,
          _this$props$onOk = _this$props.onOk,
          onOk = _this$props$onOk === void 0 ? function (e) {
        return e;
      } : _this$props$onOk;
      var selectedRowKeys = this.state.selectedRowKeys;
      var rowSelection = {
        selectedRowKeys: selectedRowKeys,
        onChange: this.handleSelectTable
      };
      var updateList = dataSource.filter(function (item) {
        return item._status === 'update';
      });
      var columns = [{
        title: intl.get('csdr.jobGroup.model.address').d('执行器地址'),
        width: 160,
        dataIndex: 'address'
      }, {
        title: intl.get('csdr.jobGroup.model.weight').d('权重'),
        width: 120,
        dataIndex: 'weight',
        render: function render(value, record) {
          if (record._status === 'create' || record._status === 'update') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(FormItem, null, getFieldDecorator('weight', {
              initialValue: value,
              rules: [{
                required: true,
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('csdr.jobGroup.model.weight').d('权重')
                })
              }]
            })( /*#__PURE__*/React.createElement(InputNumber, {
              min: 0
            })));
          } else {
            return value;
          }
        }
      }, {
        title: intl.get('csdr.jobGroup.model.maxConcurrent').d('最大并发量'),
        width: 120,
        dataIndex: 'maxConcurrent',
        render: function render(value, record) {
          if (record._status === 'create' || record._status === 'update') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(FormItem, null, getFieldDecorator('maxConcurrent', {
              initialValue: value
            })( /*#__PURE__*/React.createElement(InputNumber, {
              min: 0
            })));
          } else {
            return value;
          }
        }
      }, {
        title: intl.get('csdr.jobGroup.model.enabledFlag').d('状态'),
        width: 80,
        dataIndex: 'enabledFlag',
        render: function render(value, record) {
          if (record._status === 'create' || record._status === 'update') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(FormItem, null, getFieldDecorator('enabledFlag', {
              initialValue: value
            })( /*#__PURE__*/React.createElement(Switch, null)));
          } else {
            return enableRender(value);
          }
        }
      }, {
        title: intl.get('cpaas.common.button.action').d('操作'),
        width: 80,
        render: function render(val, record) {
          var operators = [];

          if (record._status === 'update') {
            operators.push({
              key: 'cancel',
              ele: /*#__PURE__*/React.createElement("a", {
                onClick: function onClick() {
                  return _this2.editRow(record, false);
                }
              }, intl.get('cpaas.common.button.cancel').d('取消')),
              len: 2,
              title: intl.get('cpaas.common.button.cancel').d('取消')
            });
          }

          if (record._status !== 'create' && record._status !== 'update') {
            operators.push({
              key: 'edit',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.configEdit"),
                  type: 'button',
                  meaning: '执行器管理-配置编辑'
                }],
                onClick: function onClick() {
                  return _this2.editRow(record, true);
                }
              }, intl.get('cpaas.common.button.edit').d('编辑')),
              len: 2,
              title: intl.get('cpaas.common.button.edit').d('编辑')
            });
          }

          if (record._status === 'create') {
            operators.push({
              key: 'delete',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                onClick: function onClick() {
                  return _this2.deleteRow(record);
                }
              }, intl.get('cpaas.common.button.delete').d('删除')),
              len: 2,
              title: intl.get('cpaas.common.button.delete').d('删除')
            });
          }

          return operatorRender(operators, record, {
            limit: 3
          });
        }
      }];
      return /*#__PURE__*/React.createElement(Modal, {
        destroyOnClose: true,
        wrapClassName: "ant-modal-sidebar-right",
        transitionName: "move-right",
        width: 720,
        title: intl.get('csdr.jobGroup.view.message.executor').d('配置'),
        visible: modalVisible,
        confirmLoading: saveLoading || deleteLoading,
        onCancel: this.handleCancel,
        footer: [/*#__PURE__*/React.createElement(ButtonPermission, {
          key: "cancel",
          onClick: onCancel
        }, intl.get('cpaas.common.button.cancel').d('取消')), /*#__PURE__*/React.createElement(ButtonPermission, {
          loading: saveLoading || deleteLoading,
          type: "primary",
          key: "save",
          disabled: updateList.length === 0,
          onClick: onOk
        }, intl.get('cpaas.common.button.ok').d('确定'))]
      }, /*#__PURE__*/React.createElement("div", {
        className: "table-operator"
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.reset"),
          type: 'button',
          meaning: '执行器管理-配置重置'
        }],
        onClick: this.handleDelete,
        loading: deleteLoading,
        disabled: selectedRowKeys.length === 0 || saveLoading
      }, intl.get('cpaas.common.button.reset').d('重置'))), /*#__PURE__*/React.createElement(EditTable, {
        bordered: true,
        rowSelection: rowSelection,
        loading: initLoading,
        rowKey: "uuid",
        columns: columns,
        scroll: {
          x: tableScrollWidth(columns)
        },
        dataSource: dataSource
      }));
    }
  }]);

  return UserModal;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "handleSelectTable", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleSelectTable"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleDelete", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handleDelete"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "editRow", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "editRow"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleCancel", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "handleCancel"), _class.prototype)), _class));
export { UserModal as default };